package com.smec.moblie.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smec.moblie.R;
import com.smec.moblie.bean.CustomersBean;
import com.smec.moblie.bean.TechnicalNoticeBean;
import com.smec.moblie.control.PublicControl;
import com.smec.moblie.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunyubo on 2017/11/28.
 */

public class TechnicalNoticeCustomerListAdapter extends BaseExpandableListAdapter {
    private List<CustomersBean> customersBeanList = new ArrayList<CustomersBean>();
    private Context context;

    public TechnicalNoticeCustomerListAdapter(Context context) {
        this.context = context;
    }

    public void setTechnicalNoticeList(List<CustomersBean> customersBeanList) {
        this.customersBeanList = customersBeanList;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return customersBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<TechnicalNoticeBean> childList = customersBeanList.get(groupPosition).getList();
        if (childList != null) {
            return childList.size();
        } else {
            return 0;
        }
    }

    @Override
    public CustomersBean getGroup(int groupPosition) {
        return customersBeanList.get(groupPosition);
    }

    @Override
    public TechnicalNoticeBean getChild(int groupPosition, int childPosition) {
        List<TechnicalNoticeBean> childList = customersBeanList.get(groupPosition).getList();
        if (childList != null) {
            return (childList.get(childPosition));
        } else {
            return null;
        }
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.adapter_technical_notice_group, null);
        } else {
            view = convertView;
        }
        CustomersBean customersBean = customersBeanList.get(groupPosition);
        TextView textViewCustomerName = (TextView) view.findViewById(R.id.textViewCustomerName);
        TextView textViewAddress = (TextView) view.findViewById(R.id.textViewAddress);
        TextView textViewCount = (TextView) view.findViewById(R.id.textViewCount);
        RelativeLayout layoutBack = (RelativeLayout) view.findViewById(R.id.layoutBack);
        textViewCustomerName.setText(customersBean.getCustomerName());
        textViewAddress.setText(customersBean.getAddress());
        if (customersBean.getList() != null) {
            textViewCount.setText(customersBean.getList().size() + "");
        }
        ImageView imageView_expand = (ImageView) view.findViewById(R.id.imageView_expand);

        int top = 0;
        if (groupPosition == 0) {
            top = UIUtil.dip2px(context, 10);
        }

        if (isExpanded) {
            imageView_expand.setImageResource(R.mipmap.image_pack_up);
            if (customersBean.getList() != null && customersBean.getList().size() > 0) {
                layoutBack.setBackgroundResource(R.drawable.shape_technical_notice_group_down);
                PublicControl.setMargins(layoutBack, UIUtil.dip2px(context, 10), top, UIUtil.dip2px(context, 10), 0);
            }
            view.setSelected(true);
        } else {
            imageView_expand.setImageResource(R.mipmap.image_expand);
            layoutBack.setBackgroundResource(R.drawable.shape_technical_notice_group_up);
            PublicControl.setMargins(layoutBack, UIUtil.dip2px(context, 10), top, UIUtil.dip2px(context, 10), UIUtil.dip2px(context, 10));
            view.setSelected(false);
        }

        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.adapter_technical_notice_child, null);
        } else {
            view = convertView;
        }
        TextView textViewEleContractNo = (TextView) view.findViewById(R.id.textViewEleContractNo);
        TextView textViewNoticeNum = (TextView) view.findViewById(R.id.textViewNoticeNum);
        TextView textViewCustomerName = (TextView) view.findViewById(R.id.textViewCustomerName);
        TextView textViewRequireCompleteDate = (TextView) view.findViewById(R.id.textViewRequireCompleteDate);
        LinearLayout layoutBack = (LinearLayout) view.findViewById(R.id.layoutBack);
        RelativeLayout layout_line = (RelativeLayout) view.findViewById(R.id.layout_line);
        if (customersBeanList.get(groupPosition).getList() != null) {
            TechnicalNoticeBean technicalNoticeBean = customersBeanList.get(groupPosition).getList().get(childPosition);
            textViewEleContractNo.setText(technicalNoticeBean.getEleContractNo());
            textViewNoticeNum.setText(technicalNoticeBean.getNoticeNum());
            textViewCustomerName.setText(technicalNoticeBean.getCustomerName());
            textViewRequireCompleteDate.setText(technicalNoticeBean.getRequireCompleteDate());
            if (childPosition == customersBeanList.get(groupPosition).getList().size() - 1) {
                layoutBack.setBackgroundResource(R.drawable.shape_technical_notice_child_last);
                PublicControl.setMargins(layoutBack, UIUtil.dip2px(context, 10), 0, UIUtil.dip2px(context, 10), UIUtil.dip2px(context, 10));
//                PublicControl.setMargins(layout_line, UIUtil.dip2px(context, 20), UIUtil.dip2px(context, 10), UIUtil.dip2px(context, 20), UIUtil.dip2px(context, 10));
                layout_line.setVisibility(View.INVISIBLE);
            } else {
                layoutBack.setBackgroundResource(R.drawable.shape_technical_notice_child);
                PublicControl.setMargins(layoutBack, UIUtil.dip2px(context, 10), 0, UIUtil.dip2px(context, 10), 0);
//                PublicControl.setMargins(layout_line, UIUtil.dip2px(context, 20), UIUtil.dip2px(context, 10), UIUtil.dip2px(context, 20), 0);
                layout_line.setVisibility(View.VISIBLE);
            }
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
